package com.netty.demo.recycle;

import io.netty.util.Recycler;
import io.netty.util.concurrent.FastThreadLocalThread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;

/**
 * @Author zhangtao
 * @create 2024/2/22 11:19
 */
public class RecycleDemo {
    private static final Recycler<Book> BOOK = new Recycler<Book>() {
        @Override
        protected Book newObject(Handle<Book> handle) {
            return new Book(handle);
        }
    };


    public static void main(String[] args) throws InterruptedException {
        System.out.println("Main thread started ...");
        Book book1 = BOOK.get();
        book1.setName("Java");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        new FastThreadLocalThread(() -> {
            System.out.println("Sub thread started ...");
            book1.setName("Python");
            book1.recycle();

            System.out.println("Sub Thread get name : " + BOOK.get().getName());
            countDownLatch.countDown();
        }, "Thread-1").start();

        countDownLatch.await();
        System.out.println("Main Thread get name : " + BOOK.get().getName());
    }
}
